﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Project_V1;

namespace Project_V1
{
    public partial class FrmKeyPad : Form
    {
        public static string input = string.Empty;
        public static bool confirm = false;
        //public static bool Lower_Upper_Convert = false;
        
        public FrmKeyPad()
        {
            
            InitializeComponent();
        }

        string VariableName = string.Empty;

        //在参数设置界面FrmParamSet,使用有参数构造方法传值
        public FrmKeyPad(string Variable)
        {
            InitializeComponent();
            this.txt_Modify.Text = Variable;//读取现在的值显示在Label
            VariableName = Variable;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            confirm = false;
            this.Dispose();
            this.Close();
        }

        //点击确定按钮后,将TextBox内的值写入PLC,(需要修改实时读取和停止线程)
        private void btn_Sure_Click(object sender, EventArgs e)
        {
            input = txt_Modify.Text;
            confirm = true;
            this.Dispose();
            this.Close();

        }

        //Enter
        private void txt_Modify_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn_Sure_Click(null, null);
            }
        }

        private void FrmKeyPad_Load(object sender, EventArgs e)
        {

        }
        private void FrmKeyPad_Shown(object sender, EventArgs e)
        {
            txt_Modify.Text = input;
            txt_Modify.SelectionStart = txt_Modify.Text.Length;
            txt_Modify.SelectionLength = 0;
            txt_Modify.Focus();
        }


        void btn_Click(object sender, EventArgs e)
        {
            Button B = (Button)sender;
            string buttonText = B.Text;
            int selectionStart = 0;// txt_Modify.SelectionStart; // 保存当前焦点位置
       

            txt_Modify.SelectionStart = txt_Modify.Text.Length;
            selectionStart = txt_Modify.SelectionStart; // 保存当前焦点位置
            txt_Modify.Text = txt_Modify.Text.Insert(selectionStart, buttonText); // 在焦点位置插入按钮文字

            //if (button41.Text == "小写")
            //{
            //    B.Text = B.Text.ToUpper();
            //}
            //else
            //{
            //    B.Text = B.Text.ToLower();
            //}

            txt_Modify.SelectionStart = selectionStart + buttonText.Length; // 设置焦点位置
            txt_Modify.Focus(); // 重新设置焦点到文本框
        }
        private void button8_Click(object sender, EventArgs e)
        {
            txt_Modify.Text = string.Empty;
            txt_Modify.Focus();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (txt_Modify.Text.Length == 0)
            {
                return;
            }
            txt_Modify.SelectionStart = txt_Modify.Text.Length;
            int selectionStart = txt_Modify.SelectionStart;
            txt_Modify.Text = txt_Modify.Text.Remove(selectionStart - 1, 1);
            txt_Modify.SelectionStart = selectionStart - 1;
            txt_Modify.Focus();
        }

        private void txt_Modify_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.' && !char.IsControl(e.KeyChar))
            //{
            //    e.Handled = true; // 取消输入
            //}

            //// 确保只能输入一个小数点
            if (e.KeyChar == '.' && ((TextBox)sender).Text.Contains('.'))
            {
                e.Handled = true; // 取消输入
            }
        }

        private void button41_Click(object sender, EventArgs e)
        {
            if(button41.Text == "小写")
            {

                foreach (Control ct in this.Controls)
                {
                    if (ct is Button)
                    {
                        ct.Text = ct.Text.ToLower();
                    }
                }
                button41.Text = "大写";
            }
            else
            {
                foreach (Control ct in this.Controls)
                {
                    if (ct is Button)
                    {
                        ct.Text = ct.Text.ToUpper();
                    }
                }
                button41.Text = "小写";
            }
        }
    }
}
